<ul id="treeDemo{$unique_id}" class="ztree"></ul>
<link rel="stylesheet" href="/static/admin/small/tree/ztree/css/metroStyle/metroStyle.css" type="text/css">
<script type="text/javascript" src="/static/admin/small/tree/ztree/js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="/static/admin/small/tree/ztree/js/jquery.ztree.excheck.js"></script>
<script type="text/javascript" src="/static/admin/small/tree/ztree/js/jquery.ztree.exedit.js"></script>
<SCRIPT type="text/javascript">
    <!--
    var setting{$unique_id} = {
        edit: {
            drag: {
                autoExpandTrigger: true,
                prev: dropPrev{$unique_id},
                inner: dropInner{$unique_id},
                next: dropNext{$unique_id}
            },
            enable: true
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            beforeDrag: beforeDrag{$unique_id},
            beforeEditName: beforeEditName{$unique_id},
            onRename: onRename{$unique_id},

            beforeRemove: beforeRemove{$unique_id},
            onRemove: onRemove{$unique_id},

            onDrop: onDrop{$unique_id}
        }
    };

    // var zNodes =[
    //     { id:1, pId:0, name:"父节点 1"},
    //     { id:11, pId:1, name:"叶子节点 1-1"},
    //     { id:12, pId:1, name:"叶子节点 1-2"},
    //     { id:13, pId:1, name:"叶子节点 1-3"},
    //     { id:2, pId:0, name:"父节点 2"},
    //     { id:21, pId:2, name:"叶子节点 2-1"},
    //     { id:22, pId:2, name:"叶子节点 2-2"},
    //     { id:23, pId:2, name:"叶子节点 2-3"},
    //     { id:3, pId:0, name:"父节点 3"},
    //     { id:31, pId:3, name:"叶子节点 3-1"},
    //     { id:32, pId:3, name:"叶子节点 3-2"},
    //     { id:33, pId:3, name:"叶子节点 3-3"}
    // ];

    var zNodes{$unique_id} = eval('{$catelog|raw}');
    var curDragNodes{$unique_id};

    function beforeDrag{$unique_id}(treeId, treeNodes) {
        curDragNodes{$unique_id} = treeNodes;
        for (var i=0,l=treeNodes.length; i<l; i++) {
            if (treeNodes[i].drag === false) {
                return false;
            }
        }
        return true;
    }

    var beforeEditNameVal{$unique_id} = '';

    function beforeEditName{$unique_id}(treeId, treeNode){
        beforeEditNameVal{$unique_id} = treeNode.name;
    }

    function onRename{$unique_id}(e, treeId, treeNode, isCancel){
        if( treeNode.name.length == 0 ){
            treeNode.name = beforeEditNameVal{$unique_id};
            $('#'+treeNode.tId+'_span').html(beforeEditNameVal{$unique_id});
            return false;
        }
        if( treeNode.name == beforeEditNameVal{$unique_id} ){
            return false;
        }
        if( treeNode.name.length > 0 && treeNode.name != beforeEditNameVal{$unique_id} )
        {
            var url = "{:url('admin/doccatelog/edit')}";
            $.ajax({
                url : url,
                async : false,//设置为同步请求
                data : treeNode,
                success : function(data) {
                    if(data.code == 0)
                    {
                        layer.msg(data.msg,{icon:2});
                        treeNode.name = beforeEditNameVal{$unique_id};
                        $('#'+treeNode.tId+'_span').html(beforeEditNameVal{$unique_id});
                    }
                },
                error: function (data){
                    layer.msg('数据返回错误',{icon:2});
                }
            }).fail(function(){
                layer.msg('网络错误',{icon:2});
            });
        }
    }

    function beforeRemove{$unique_id}(treeId, treeNode){
        return confirm("此操作不可恢复，确定继续吗？");
    }

    function onRemove{$unique_id}(e, treeId, treeNode){
        var data = {'id':treeNode.id}
        var url = "{:url('admin/doccatelog/del')}";
        $.ajax({
            url : url,
            async : false,//设置为同步请求
            data : data,
            success : function(res) {
                if(res.code == 0)
                {
                    layer.msg(res.msg,{icon:2});
                }
            },
            error: function (res){
                layer.msg('数据返回错误',{icon:2});
            }
        }).fail(function(){
            layer.msg('网络错误',{icon:2});
        });
    }

    function onDrop{$unique_id}(event, treeId, treeNodes, targetNode, moveType, isCopy){
        var url = "{:url('admin/docCatelog/edit_sort')}";
        var data = {id:treeNodes[0].id,target_id:targetNode.id,pid:targetNode.pid,move_type:moveType,doc_id:targetNode.doc_id};
        $.ajax({
            url:url,
            data:data,
            success:function(res){
                console.log(res);
            },
            error:function (res) {
                layer.msg('数据返回错误',{icon:2});
            }
        }).fail(function (res) {
            // layer.msg('网络错误',{icon:2});
        });

        // var zTree = $.fn.zTree.getZTreeObj("treeDemo{$unique_id}");
        // console.log(zTree);
    }

    function dropPrev{$unique_id}(treeId, nodes, targetNode) {
        var pNode = targetNode.getParentNode();
        if (pNode && pNode.dropInner === false) {
            return false;
        } else {
            for (var i=0,l=curDragNodes{$unique_id}.length; i<l; i++) {
                var curPNode = curDragNodes{$unique_id}[i].getParentNode();
                if (curPNode && curPNode !== targetNode.getParentNode() && curPNode.childOuter === false) {
                    return false;
                }
            }
        }
        return true;
    }
    function dropInner{$unique_id}(treeId, nodes, targetNode) {
        return false;
        // if (targetNode && targetNode.dropInner === false) {
        //     return false;
        // } else {
        //     for (var i=0,l=curDragNodes{$unique_id}.length; i<l; i++) {
        //         if (!targetNode && curDragNodes{$unique_id}[i].dropRoot === false) {
        //             return false;
        //         } else if (curDragNodes{$unique_id}[i].parentTId && curDragNodes{$unique_id}[i].getParentNode() !== targetNode && curDragNodes{$unique_id}[i].getParentNode().childOuter === false) {
        //             return false;
        //         }
        //     }
        // }
        // return true;
    }
    function dropNext{$unique_id}(treeId, nodes, targetNode) {
        var pNode = targetNode.getParentNode();
        if (pNode && pNode.dropInner === false) {
            return false;
        } else {
            for (var i=0,l=curDragNodes{$unique_id}.length; i<l; i++) {
                var curPNode = curDragNodes{$unique_id}[i].getParentNode();
                if (curPNode && curPNode !== targetNode.getParentNode() && curPNode.childOuter === false) {
                    return false;
                }
            }
        }
        return true;
    }

    $(document).ready(function(){
        $.fn.zTree.init($("#treeDemo{$unique_id}"), setting{$unique_id}, zNodes{$unique_id});
    });
    //-->
</SCRIPT>